package com.xiemingtian.navdemo


import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xiemingtian.navdemo.home.MainFragment
import com.xiemingtian.navdemo.home.MeFragment
import kotlinx.android.synthetic.main.fragment_home.*


class HomeFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val list = ArrayList<Fragment>()
        val mainFragment = MainFragment()
        val meFragment = MeFragment()

        mainFragment.arguments = arguments
        meFragment.arguments = arguments

        list.add(mainFragment)
        list.add(meFragment)
        pager.adapter = HomePagerAdapter(list, childFragmentManager)
        tabbar.setupWithViewPager(pager)
    }

    private class HomePagerAdapter(private val fragment: ArrayList<Fragment>,fragmentManager: FragmentManager) : FragmentPagerAdapter(fragmentManager){
        override fun getItem(position: Int): Fragment {
            return fragment[position]
        }

        override fun getCount(): Int {
            return fragment.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return when(position){
                0 -> "Main"
                else -> "Me"
            }
        }

    }

}
